<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class UserUpdateForm extends UserFull
{
    
    public function save()
    {
        if($this->validate()){
                        if($this->validateEmail()){
                $user = User::model()->findByPk($this->id);
                if(isset($this->password) && !empty($this->password) && $this->oldPassword!=$this->password){
                    $user["user_pass"] = CSecurity::signPasswd($this->password, $user["salt"]);
                }
                $user["user_status"] = $this->status;
                $result = $user->save();
                                if(isset($this->email) && $this->email !== $this->oldEmail){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'email'));
                    if (empty($userMeta))
                    {
                        $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="email";
                    }
                    $userMeta["meta_value"]=$this->email;
                    $userMeta->save();
                    unset($userMeta);
                }
                if(isset($this->nick) &&$this->nick !== $this->oldNick){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'nick'));
                    if (empty($userMeta))
                    {
                        $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="nick";

                    }
                    $userMeta["meta_value"]=$this->nick;
                    $userMeta->save();
                    unset($userMeta);
                }
                if(isset($this->isAdmin) && $this->isAdmin !== $this->oldIsAdmin){
                                        $isLastAdmin = false;
                    if($this->oldIsAdmin=="1"){                        $meta = new UserMeta();
                        $isLastAdmin = $meta->isLastAdmin($this->id);
                    }
                    if($isLastAdmin==false){                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'is_admin'));
                        if (empty($userMeta))
                        {
                            $userMeta = new UserMeta();
                            $userMeta["user_id"]=$this->id;
                            $userMeta["meta_key"]="is_admin";
                        }
                        $userMeta["meta_value"]=$this->isAdmin;
                        $userMeta->save();
                        unset($userMeta);
                    }
                }
                if(isset($this->space) && $this->space !== $this->oldSpace){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'space'));
                    if (empty($userMeta))
                    {
                                                $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="space";
                    }
                    $userMeta["meta_value"]=$this->space;
                    $userMeta->save();
                    unset($userMeta);
                }
                if(isset($this->realName) && $this->realName !== $this->oldRealName){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'realname'));
                    if (empty($userMeta))
                    {
                        $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="realname";

                    }
                    $userMeta["meta_value"]=$this->realName;
                    $userMeta->save();
                    unset($userMeta);
                }
                if(isset($this->phone) && $this->phone !== $this->oldPhone){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'phone'));
                    if (empty($userMeta))
                    {
                        $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="phone";
                    }
                    $userMeta["meta_value"]=$this->phone;
                    $userMeta->save();
                    unset($userMeta);
                }
                if(isset($this->qq) && $this->qq !== $this->oldQq){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'qq'));
                    if (empty($userMeta))
                    {
                        $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="qq";
                    }
                    $userMeta["meta_value"]=$this->qq;
                    $userMeta->save();
                    unset($userMeta);
                }
                if(isset($this->website) && $this->website !== $this->oldWebsite){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'website'));
                    if (empty($userMeta))
                    {
                        $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="website";
                    }
                    $userMeta["meta_value"]=$this->website;
                    $userMeta->save();
                    unset($userMeta);
                }
                if(isset($this->memo) && $this->memo !== $this->oldMemo){
                                        $userMeta= UserMeta::model()->findByAttributes(array('user_id'=>$this->id, 'meta_key'=>'memo'));
                    if (empty($userMeta))
                    {
                        $userMeta = new UserMeta();
                        $userMeta["user_id"]=$this->id;
                        $userMeta["meta_key"]="memo";
                    }
                    $userMeta["meta_value"]=$this->memo;
                    $userMeta->save();
                    unset($userMeta);
                }

                                if (Yii::app()->session["user"]["id"] == $this->id){
                    $userBase = new UserBase();
                    $userBase->load($this->id);
                    Yii::app()->session["user"]=$userBase->serialize();
                }
                return true;
            }
        }
        return false;
    }
}